home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / chat / talkd.000 / talkd / announce.c next >
Encoding:
C/C++ Source or Header  |  1994-12-14  |  6.2 KB  |  205 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)announce.c    5.9 (Berkeley) 2/26/91";
  36. #endif /* not lint */
  37.  
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <sys/time.h>
  41. #include <sys/wait.h>
  42. #include <sys/socket.h>
  43. #include <protocols/talkd.h>
  44. #include <sgtty.h>
  45. #include <errno.h>
  46. #include <syslog.h>
  47. #include <unistd.h>
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include <paths.h>
  51.  
  52. extern char hostname[];
  53.  
  54. /*
  55.  * Announce an invitation to talk.
  56.  *
  57.  * Because the tty driver insists on attaching a terminal-less
  58.  * process to any terminal that it writes on, we must fork a child
  59.  * to protect ourselves
  60.  */
  61. announce(request, remote_machine)
  62.     CTL_MSG *request;
  63.     char *remote_machine;
  64. {
  65.     int pid, val, status;
  66.  
  67.     if ((strstr(request->l_name,"\033"))||(strstr(remote_machine,"\033")))
  68.     {
  69.         if (strstr(remote_machine,"\033"))
  70.         {
  71.             syslog(LOG_WARNING, "blocked VT100 BOMB to user: %s (from field in packet contains bomb)", request->r_name);
  72.         } else {
  73.             syslog(LOG_WARNING, "blocked VT100 BOMB to user: %s (apparently from: %s)", request->r_name, remote_machine);
  74.         }
  75.     } else {
  76.         if (pid = fork()) {
  77.             /* we are the parent, so wait for the child */
  78.             if (pid == -1)        /* the fork failed */
  79.                 return (FAILED);
  80.             do {
  81.                 val = wait(&status);
  82.                 if (val == -1) {
  83.                     if (errno == EINTR)
  84.                         continue;
  85.                     /* shouldn't happen */
  86.                     syslog(LOG_WARNING, "announce: wait: %m");
  87.                     return (FAILED);
  88.                 }
  89.             } while (val != pid);
  90.             if (status&0377 > 0)    /* we were killed by some signal */
  91.                 return (FAILED);
  92.             /* Get the second byte, this is the exit/return code */
  93.             return ((status >> 8) & 0377);
  94.         }
  95.         /* we are the child, go and do it */
  96.         _exit(announce_proc(request, remote_machine));
  97.     }
  98. }
  99.     
  100. /*
  101.  * See if the user is accepting messages. If so, announce that 
  102.  * a talk is requested.
  103.  */
  104. announce_proc(request, remote_machine)
  105.     CTL_MSG *request;
  106.     char *remote_machine;
  107. {
  108.     int pid, status;
  109.     char full_tty[32];
  110.     FILE *tf;
  111.     struct stat stbuf;
  112.  
  113.     (void)sprintf(full_tty, "%s/%s", _PATH_DEV, request->r_tty);
  114.     if (access(full_tty, 0) != 0)
  115.         return (FAILED);
  116.     if ((tf = fopen(full_tty, "w")) == NULL)
  117.         return (PERMISSION_DENIED);
  118.     /*
  119.      * On first tty open, the server will have
  120.      * it's pgrp set, so disconnect us from the
  121.      * tty before we catch a signal.
  122.      */
  123.     ioctl(fileno(tf), TIOCNOTTY, (struct sgttyb *) 0);
  124.     if (fstat(fileno(tf), &stbuf) < 0)
  125.         return (PERMISSION_DENIED);
  126.     if ((stbuf.st_mode&020) == 0)
  127.         return (PERMISSION_DENIED);
  128.     print_mesg(tf, request, remote_machine);
  129.     fclose(tf);
  130.     return (SUCCESS);
  131. }
  132.  
  133. #define max(a,b) ( (a) > (b) ? (a) : (b) )
  134. #define N_LINES 5
  135. #define N_CHARS 120
  136.  
  137. /*
  138.  * Build a block of characters containing the message. 
  139.  * It is sent blank filled and in a single block to
  140.  * try to keep the message in one piece if the recipient
  141.  * in in vi at the time
  142.  */
  143. print_mesg(tf, request, remote_machine)
  144.     FILE *tf;
  145.     CTL_MSG *request;
  146.     char *remote_machine;
  147. {
  148.     struct timeval clock;
  149.     struct timezone zone;
  150.     struct tm *localtime();
  151.     struct tm *localclock;
  152.     char line_buf[N_LINES][N_CHARS];
  153.     int sizes[N_LINES];
  154.     char big_buf[N_LINES*N_CHARS];
  155.     char *bptr, *lptr;
  156.     int i, j, max_size;
  157.  
  158.     i = 0;
  159.     max_size = 0;
  160.     gettimeofday(&clock, &zone);
  161.     localclock = localtime( &clock.tv_sec );
  162.     (void)sprintf(line_buf[i], " ");
  163.     sizes[i] = strlen(line_buf[i]);
  164.     max_size = max(max_size, sizes[i]);
  165.     i++;
  166.     (void)sprintf(line_buf[i], "Message from Talk_Daemon@%s at %d:%02d ...",
  167.     hostname, localclock->tm_hour , localclock->tm_min );
  168.     sizes[i] = strlen(line_buf[i]);
  169.     max_size = max(max_size, sizes[i]);
  170.     i++;
  171.     (void)sprintf(line_buf[i], "talk: connection requested by %s@%s.",
  172.         request->l_name, remote_machine);
  173.     sizes[i] = strlen(line_buf[i]);
  174.     max_size = max(max_size, sizes[i]);
  175.     i++;
  176.     (void)sprintf(line_buf[i], "talk: respond with:  talk %s@%s",
  177.         request->l_name, remote_machine);
  178.     sizes[i] = strlen(line_buf[i]);
  179.     max_size = max(max_size, sizes[i]);
  180.     i++;
  181.     (void)sprintf(line_buf[i], " ");
  182.     sizes[i] = strlen(line_buf[i]);
  183.     max_size = max(max_size, sizes[i]);
  184.     i++;
  185.     bptr = big_buf;
  186.     *bptr++ = ''; /* send something to wake them up */
  187.     *bptr++ = '\r';    /* add a \r in case of raw mode */
  188.     *bptr++ = '\n';
  189.     for (i = 0; i < N_LINES; i++) {
  190.         /* copy the line into the big buffer */
  191.         lptr = line_buf[i];
  192.         while (*lptr != '\0')
  193.             *(bptr++) = *(lptr++);
  194.         /* pad out the rest of the lines with blanks */
  195.         for (j = sizes[i]; j < max_size + 2; j++)
  196.             *(bptr++) = ' ';
  197.         *(bptr++) = '\r';    /* add a \r in case of raw mode */
  198.         *(bptr++) = '\n';
  199.     }
  200.     *bptr = '\0';
  201.     fprintf(tf, big_buf);
  202.     fflush(tf);
  203.     ioctl(fileno(tf), TIOCNOTTY, (struct sgttyb *) 0);
  204. }
  205.